package cn.yjr.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.tools.BitmapHelper;
import cn.edu.pku.tools.YJRApplication;
import cn.edu.pku.tools.YJRHttpClient;
import cn.edu.pku.tools.FileHelper;
import cn.edu.pku.tools.SharedPreferencesHelper;
import cn.liuyi.weiyi.AccountActivity;
import cn.liuyi.weiyi.MyBalanceActivity;
import cn.liuyi.weiyi.R;
import cn.liuyi.weiyi.TransactionRecordActivity;

import com.actionbarsherlock.app.SherlockFragment;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class PersonalCenterFragment extends SherlockFragment
  implements View.OnClickListener
{
  RelativeLayout account_rl;
  Context context;
  FileHelper fileHelper;
  String gender = "男";
  ImageView head_imv;
  Bitmap header_bitmap;
  JSONObject userData;
  
  SharedPreferencesHelper helper;
  RelativeLayout my_balance_rl;
  RelativeLayout my_registration_rl;
  String name = "向北向南61";
  String phone_num = "15201317795",head_path;
  RelativeLayout transaction_record_rl;
  TextView name_tv,telphone_tv;
  View view;
  DisplayImageOptions options;
  protected ImageLoader imageLoader = ImageLoader.getInstance();
  public PersonalCenterFragment(){
	  
  }
  private void initData()
  {
    this.header_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);
//    this.nick_name = this.helper.getStringValue("nick_name", "无");
//    this.phone_num = this.helper.getStringValue("phone");
    Bitmap localBitmap = this.fileHelper.readPic("pic/head_thumbail.jpeg");
    if (localBitmap == null)
      return;
    this.header_bitmap = localBitmap;
  }

  void initComponent()
  {
    this.head_imv = ((ImageView)this.view.findViewById(R.id.my_head));
    this.head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(this.header_bitmap));
    this.account_rl = ((RelativeLayout)this.view.findViewById(R.id.account_rl));
    this.transaction_record_rl = ((RelativeLayout)this.view.findViewById(R.id.transaction_record_rl));
    this.my_registration_rl = ((RelativeLayout)this.view.findViewById(R.id.my_registration_rl));
    this.my_balance_rl = ((RelativeLayout)this.view.findViewById(R.id.my_balance_rl));
    this.account_rl.setOnClickListener(this);
    this.transaction_record_rl.setOnClickListener(this);
    this.my_registration_rl.setOnClickListener(this);
    this.my_balance_rl.setOnClickListener(this);
    name_tv = (TextView) view.findViewById(R.id.name_tv);
    telphone_tv = (TextView) view.findViewById(R.id.telphone_tv);
  }

  public void onClick(View paramView)
  {
	Intent intent;
	switch (paramView.getId())
    {
    case R.id.account_rl:
    	intent = new Intent(this.context, AccountActivity.class);
        this.context.startActivity(intent);
        break;
    case R.id.transaction_record_rl:
//    	intent = new Intent(this.context, TransactionRecordActivity.class);
//        this.context.startActivity(intent);
        break;
    case R.id.my_balance_rl:
    	intent = new Intent(this.context, MyBalanceActivity.class);
        this.context.startActivity(intent);
        break;
    case R.id.my_registration_rl:
//    	intent = new Intent(this.context, MyRegistrationActivity.class);
//        this.context.startActivity(intent);
        break;
    default:
    	break;
    }
  }

  public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
  {
    this.view = paramLayoutInflater.inflate(R.layout.fragment_personal_center, paramViewGroup, false);
    this.context = getActivity();
    this.fileHelper = new FileHelper(this.context);
    this.helper = new SharedPreferencesHelper(this.context);
    initData();
    initComponent();
    getUserData();
    return this.view;
  }
  
  void setComponent(){
		//this.head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(this.header_bitmap));
		name_tv.setText(name);
		telphone_tv.setText(phone_num);
		//imageLoader.displayImage("http://115.28.222.1/yijiaren/pics/avatar/2014/04/23/139818550443800002.jpg", head_imda, options);
		imageLoader.loadImage("http://115.28.222.1/yijiaren/pics/avatar/"+head_path,options, new ImageLoadingListener(){

			@Override
			public void onLoadingCancelled(String arg0, View arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
				// TODO Auto-generated method stub
				head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(arg2));
				//head_imv.setImageBitmap(arg2);
			}

			@Override
			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLoadingStarted(String arg0, View arg1) {
				// TODO Auto-generated method stub
				
			}

			
		});
  }
  public void getUserData(){
		RequestParams params = new RequestParams(); 
		YJRHttpClient.post(context,"/user/refreshUser.action", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	try {
						if(data.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "获取用户信息成功", Toast.LENGTH_SHORT).show();
							userData = data.getJSONObject("response");
							name = userData.getString("name");
							phone_num = userData.getString("phone");
							head_path = userData.getString("avatar");

							YJRApplication.avatar_path = head_path;
							//String avatar_path = userData.getString("avatar");
							//String host = ApplicationData.userData.getJSONObject("imagePaths").getString("avatarPath");
							//String head_path = host + avatar_path;
							setComponent();
							//TODO 将用户信息存储在本地
								
						}else{
							Toast.makeText(context, data.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
  }
}